package com.ivr.cm.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.ivr.cm.dao.IUserCallinDao;
import com.ivr.cm.service.BaseServiceIF;
import com.ivr.cm.util.BaseUtil;
import com.ivr.cm.vo.UserCallinVo;

/***
 * 呼入记录数据实现
 * @author liugeng
 * @date 2015-12-15
 * @version 1.0
 */
public class UserCallinDaoImpl extends JdbcDaoSupport implements IUserCallinDao,BaseServiceIF<Object> {
	private static Logger logger = Logger.getLogger(UserCallinDaoImpl.class);

	@Override
	public int doProcess(Object obj) {
		if(obj instanceof UserCallinVo){
			return insertUserCallin((UserCallinVo)obj);
		}
		return 0;
	}
	/**保存呼入记录日志*/
	@Override
	public int insertUserCallin(UserCallinVo userCallinVo) {
		String sql = "insert into tb_ivr_user_callin(mobile,sp_number,province,city,state_code,area_code,isfilter,linkId,add_date) values(?,?,?,?,?,?,?,?,now())";
		Object[] param =new Object[]{userCallinVo.getMobile(),userCallinVo.getSp_number(),
									userCallinVo.getProvince(),userCallinVo.getCity(),
									userCallinVo.getState_code(),userCallinVo.getArea_code(),
									userCallinVo.getIsfilter(),userCallinVo.getLinkId()};
		logger.info(String.format("保存呼入记录日志[%s]", BaseUtil.logSQL(sql, param)));
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	
	/**获取20分钟前的呼入记录*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getUserCallinListByTime() {
		List<Object[]> list = new ArrayList<Object[]>();
		
		String sql = "select mobile, add_date, IFNULL(isfilter,0) as isfilter, linkId from tb_ivr_user_callin where add_date > DATE_ADD(now(), INTERVAL -45 MINUTE) order by add_date";
		List<Map<String, Object>> listResult = this.getJdbcTemplate().queryForList(sql);
		
		if(listResult != null && listResult.size() > 0){
			Iterator<Map<String, Object>> iterator = listResult.iterator();
			Map<String, Object> map = null;
			while(iterator.hasNext()){
				map = iterator.next();
				list.add(new Object[]{map.get("mobile"),map.get("add_date"),map.get("isfilter"),map.get("linkId")});
			}
		}
		return list;
	}
}
